import { Refund } from 'refund-model.js';
import { Order } from '../order/order-model.js';
var refund = new Refund();
var order = new Order();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showInput: false,
    statusType: ["售后申请", "处理中", "已处理"],
    currentTpye: 0,
    page: 1,
    log: [],
    hiddenNullData: true,
    hiddenNomore: true,
    hiddenLoading: true,
    onLoading: false,
    nomore: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var index = options.index ? options.index : 0;
    this.setData({
      currentTpye : index,
      log : []
    })
    //退款列表
    this.getData();
  },
  getData() {
    var curType = this.data.currentTpye;
    if (curType == 0) {
      this._loadData();
    } else if (curType == 1) {
      this.getLog(2);
    } else if (curType == 2) {
      this.getLog(1);
    }
  },
  _loadData: function() {
    var that = this;
    //是否正在加载数据或已加载完全部数据
    if (this.data.onLoading || this.data.nomore) {
      return false;
    }
    var args = {
      page: this.data.page,
    }
    //显示正在加载中
    this.setData({
      hiddenLoading: false
    })
    this.data.onLoading = true;
    refund.getOrdersRufund(args, (res) => {
      that.data.onLoading = false;
      var log = that.data.log.concat(res.items);
      that.setData({
        log
      })
      //隐藏正在加载中
      that.setData({
        hiddenLoading: true
      })
      if (log.length == 0) {
        //显示没有数据
        that.setData({
          hiddenNullData: false
        })
      }
      if (that.data.page >= res._meta.pageCount) {
        that.data.nomore = true;
        if (that.data.page > 1) {
          //显示没有更多
          that.setData({
            hiddenNomore: false
          })
        }
      }
    });
  },
  //申请记录
  getLog:function(status = ''){
    var that = this;
    //是否正在加载数据或已加载完全部数据
    if (this.data.onLoading || this.data.nomore) {
      return false;
    }
    var args = {
      status : status,
      page: this.data.page,
    }
    //显示正在加载中
    this.setData({
      hiddenLoading: false
    })
    this.data.onLoading = true;
    refund.getLog(args, (res) => {
      that.data.onLoading = false;
      var log = that.data.log.concat(res.items);
      that.setData({
        log
      })
      //隐藏正在加载中
      that.setData({
        hiddenLoading: true
      })
      if (log.length == 0) {
        //显示没有数据
        that.setData({
          hiddenNullData: false
        })
      }
      if (that.data.page >= res._meta.pageCount) {
        that.data.nomore = true;
        if (that.data.page > 1) {
          //显示没有更多
          that.setData({
            hiddenNomore: false
          })
        }
      }
    });
  },
  statusTap:function(e){
    var curType = e.currentTarget.dataset.index;
    this.data.currentTpye = curType
    this.setData({
      currentTpye: curType
    });
    //重置数据
    this.setData({
      page : 1,
      hiddenNullData : true,
      hiddenNomore : true,
      nomore: false,
      log : []
    })
    this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if(this.data.nomore){
      this.setData({
        hiddenNomore: false,
      })
      return false;
    }
    var that = this; 
    this.data.page++;
    this.getData();
  },
 
  //订单详情
  goTodetails: function (event) {
    var type = refund.getDataSet(event, 'type');
    var id = refund.getDataSet(event, 'id');
    wx.navigateTo({
      url: 'details?type='+type+'&id='+id,
    })
  },
  //跳转到填写发货单页面
  gotoFahuoNum:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'./fahuo-num?id='+id
    })
  },
  toApply: function (event){
    var sku_id = refund.getDataSet(event, 'sku_id');
    var order_id = refund.getDataSet(event, 'order_id');
    wx.navigateTo({
      url: './apply?sku_id=' + sku_id+'&order_id='+order_id
    })
  },
  //显示弹窗
  showModal:function(e){
    this.setData({
      showInput: true
    })
  },
  //关闭弹窗
  hideModal: function (e) {
    this.setData({
      showInput: false
    })
  },
})